using NUnit.Framework;

namespace Solver
{
	/// <summary>
	/// Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
	/// </summary>
	public class Problem002 : SelfChecker, IProblem
	{
		public int Id
		{
			get { return 2; }
		}

		public string Solve()
		{
			return Solve(4000000).ToString();
		}

		[Test]
		public override void Check()
		{
			Assert.AreEqual(44, Solve(100));
			Assert.AreEqual(4613732, Solve(4000000));
		}

		private static int Solve(int n)
		{
			int f1 = 1;
			int f2 = 2;
			int sum = 2;
			int even = 3;
			while (true)
			{
				var f = f1 + f2;
				f1 = f2;
				f2 = f;
				if (f2 >= n) break;
				if (--even == 0)
				{
					even = 3;
					sum += f;
				}
			}
			return sum;
		}
	}
}